S = {}
window.S = S

moment.locale('zh-cn')

S.alert = (message...)-> alert(message...)

S.trigger = (eventName, eventData)->
    $(window).trigger eventName, eventData

S.on = (eventName, eventHandler)->
    $(window).on eventName, eventHandler

S.setTimeout = (timeout, callback)-> setTimeout(callback, timeout)

S.getAppError = (xhr)->
    try
        JSON.parse xhr.responseText
    catch
        null

S.alertAjaxError = (xhr)->
    ae = S.getAppError xhr
    if ae
        S.alert ae.defaultMessage || ae.message
    else if xhr.status == 403
        S.alert "权限不够！"
    else
        S.alert "服务器错误（#{xhr.status}）"

S.hasPermit = (p)->
    permissions = S.user?.permissions || {}
    _.includes permissions, p

